<?php
include '../config.php';
include '../classes/BadgeClass.php';
include 'utils.php';

session_start();

$location = "Location: " . $ISSUER_HOST . "/create_badge_result.php";

// Connecting, selecting database
$mysqli = new mysqli($MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DB_NAME);
if ($mysqli->connect_errno) {
	$_SESSION[$CB_MSG_TAG] = $DB_CONN_ERR;
	$_SESSION[$DB_CONN_ERRNO] = $mysqli->connect_errno;
	$_SESSION[$DB_CONN_ERROR] = $mysqli->connect_error;
	header($location);
	exit();
}

//  if ($_SESSION["imageError"] > 0) {
//  	$mysqli->close();
//  	$_SESSION[$CB_MSG_TAG] = $UPLOAD_ERR;
//  	$_SESSION[$UPLOAD_ERROR] = $_SESSION["imageError"];
//  	header($location);
//  	exit();
//  }
 
 $name = $_SESSION["nomeBadge"];
	
 $nameForQuery = prepareString($mysqli, $_SESSION["nomeBadge"]);
 $description = prepareString($mysqli, $_SESSION["descrizioneBadge"]);
 $tags = prepareString($mysqli, $_SESSION["tagsBadge"]);

 /*$allowedExts = array("png");
 $extension = strtolower(end(explode(".", $_SESSION["imageName"])));

 if ((($_SESSION["imageType"] == "image/x-png")
 	|| ($_SESSION["imageType"] == "image/png"))
 	&& in_array($extension, $allowedExts)) {
	
	 	$created = move_uploaded_file($_SESSION["imageTmpName"],
    	$BADGE_IMG_PATH_FROM_PHP . "/" . $name . ".png");
	
 	if (!$created) {
 		unlink($BADGE_IMG_PATH_FROM_PHP . "/" . $name . ".png");
 		$mysqli->close();
 		$_SESSION[$CB_MSG_TAG] = $IMG_FILE_ERR;
 		header($location);
 		exit();
 	}
	
 } else {
 	$_SESSION[$CB_MSG_TAG] = $WRONG_IMG_FORMAT;
 	header($location);
 	exit();
 }*/

 // Create new badge json file
 $file_name = "badge_" . $name . ".json";

 $badge_asser = new BadgeClass();
 $badge_asser->setName($name);
 $badge_asser->setDescription($description);
 $badge_asser->setImage($ISSUER_HOST . "/img/badges_png/" . $name . ".png");
 $badge_asser->setCriteria($ISSUER_HOST . "/badges.php");
 $badge_asser->setIssuer( $JSON_FILES_URL . "/org.json");
 $badge_asser->setTags(preg_split("/ /", $tags));

 $json_string = json_encode($badge_asser->getArray());

 $created = createFile($JSON_BADGES_PATH_FROM_PHP, $file_name, $json_string);

 if (!$created) {
 	unlink($BADGE_IMG_PATH_FROM_PHP . "/" . $name . ".png");
 	unlink($JSON_BADGES_PATH_FROM_PHP . "/" . $file_name);
 	$mysqli->close();
 	$_SESSION[$CB_MSG_TAG] = $FILE_ERR;
 	header($location);
 	exit();
 }

 $image = "img/badges_png/" . $name . ".png";
 if (isset($_SESSION["condivisaBadge"]))
 	$condivisa = TRUE;
 else
 	$condivisa = FALSE;

 $issuer = $_SESSION["id_issuer"];
 $categoria = $_SESSION["categoriaBadge"];

 // Performing SQL query
 $query = sprintf($INSERT_BADGE, $nameForQuery, $description, $image, $tags, $issuer, $condivisa, $categoria);

 $result = $mysqli->query($query);

 if (!$result) {
 	unlink($BADGE_IMG_PATH_FROM_PHP . "/" . $name . ".png");
 	unlink($JSON_BADGES_PATH_FROM_PHP . "/" . $file_name);
 	mysqli_free_result($result);
 	$mysqli->close();
 	$_SESSION[$CB_MSG_TAG] = $QUERY_ERR;
 	$_SESSION[$QUERY_TAG] = $query;
	$_SESSION[$QUERY_ERRNO] = $mysqli->errno;
 	$_SESSION[$QUERY_ERROR] = $mysqli->error;
 	header($location);
 	exit();
 }

 mysqli_free_result($result);
 
 if ($_SESSION['criteria']== true) {
 	$query_badge = "SELECT badgeId FROM badge WHERE nome='" . $nameForQuery . "'" ;
 	$result_id = $mysqli->query($query_badge);
 	$row = $result_id->fetch_assoc();
 	$id = $row["badgeId"];
 	$num = count($_POST["tipo"]);
 	for ($i = 0; $i < $num; $i++) {
 		$tipo = $_POST["tipo"][$i];
 		$contenuto = $_POST["contenuto"][$i];
		echo $tipo . " " . $contenuto . "<br>";
 		$insert_query = sprintf($INSERT_CRITERIA, $id, $tipo, $contenuto);
 		$result_criteria = $mysqli->query($insert_query);
 	}
 	mysqli_free_result($result_criteria);
 	
 }
 // Closing connection
 mysqli_free_result($result_id);
 $mysqli->close();

$_SESSION[$CB_MSG_TAG] = $MSG_OK;
header($location);
exit();
?>
